package weaver.batchchanges.cmd;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import weaver.conn.RecordSet;
import weaver.batchchanges.bean.BatchTasksChangesGlobalData;
import weaver.batchchanges.bean.TaskHasChangedBean;
import weaver.batchchanges.util.CmdResultUtil;

import java.util.List;


/**
 * 获取任务发生变动的数据cmd
 */
public class GetTaskHasChangedDataCmd extends AbstractCommonCommand<List<TaskHasChangedBean>> {


    @Override
    public BizLogContext getLogContext() {
        return null;
    }

    @Override
    public List<TaskHasChangedBean> execute(CommandContext commandContext) {
        RecordSet rs = new RecordSet();

        /*
        * select old.touchpointbh                as touchpointbh,
                 old.rwmc                        as rwmc,       # requestId
                 old.hdcjr                       as old_hdcjr,
                 old.id                       as old_id,
                 new.id                       as new_id,
          #        old.bgrwjd                      as old_bgrwjd,
                 new.hdcjr                       as new_hdcjr,
                 IF(new.bgrwjd IS NULL OR new.bgrwjd = '',
                    (select n.NOWNODEID from workflow_nownode as n where n.REQUESTID = old.rwmc limit 1),
                    new.bgrwjd)                  as new_bgrwjd, # 当前nodeId，如果批量变更任务台账表里没有需要变更任务节点的nodeId，则获取流程的当前node
                 (select rb.WORKFLOWID
                  from workflow_requestbase as rb
                  where new.rwmc = rb.REQUESTID) as workflowId  #通过表里存的requestId获取workflowId
          from uf_plbgrw as new, # 批量变更任务台账表
               uf_rwtzeq as old  # 任务台账二期表
          where new.touchpointbh = old.touchpointbh
        * */
        String sql = "select old.touchpointbh                as touchpointbh,\n" +
                "       old.rwmc                        as rwmc,       # requestId\n" +
                "       old.hdcjr                       as old_hdcjr,\n" +
                "       new.id                       as new_id,\n" +
                "       old.id                       as old_id,\n" +
                "#        old.bgrwjd                      as old_bgrwjd,\n" +
                "       new.hdcjr                       as new_hdcjr,\n" +
                "       IF(new.bgrwjd IS NULL OR new.bgrwjd = '',\n" +
                "          (select n.NOWNODEID from workflow_nownode as n where n.REQUESTID = old.rwmc limit 1),\n" +
                "          new.bgrwjd)                  as new_bgrwjd, # 当前nodeId，如果批量变更任务台账表里没有需要变更任务节点的nodeId，则获取流程的当前node\n" +
                "       (select rb.WORKFLOWID\n" +
                "        from workflow_requestbase as rb\n" +
                "        where new.rwmc = rb.REQUESTID) as workflowId  #通过表里存的requestId获取workflowId\n" +
                "from " + BatchTasksChangesGlobalData.TEMPORARY_TASK_LEDGER_TABLE_NAME + " as new, # 批量变更任务台账表\n" +
                "     " + BatchTasksChangesGlobalData.TASK_LEDGER_PHASE2_TABLE_NAME + " as old  # 任务台账二期表\n" +
                "where new.touchpointbh = old.touchpointbh";
        // 防止sql注入, objects 为动态参数
        rs.executeQuery(sql);
        List<TaskHasChangedBean> listMapResult = CmdResultUtil.getResult(rs, TaskHasChangedBean.class);

        return listMapResult;
    }
}
